package com.sicilin.common.json.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Calendar;

public class TimestampSerializer extends JsonSerializer<Timestamp> {
    public void serialize(Timestamp timestamp, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        Calendar c = Calendar.getInstance();
        c.setTime(timestamp);
        jsonGenerator.writeObject((new JSONObject()).element("year", c.get(1)).element("month", c.get(2)).element("day", c.get(5)).element("hours", c.get(11)).element("minutes", c.get(12)).element("seconds", c.get(13)).element("milliseconds", c.get(14)));
    }
}
